package vn.edu.saigontech.ncms.web.offering;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import vn.edu.saigontech.ncms.model.enumeration.TeacherType;
import vn.edu.saigontech.ncms.model.info.Offering;
import vn.edu.saigontech.ncms.model.info.Teacher;
import vn.edu.saigontech.ncms.model.teaching.TeachingSwitch;
import vn.edu.saigontech.ncms.service.OfferingService;
import vn.edu.saigontech.ncms.service.TeacherService;
import vn.edu.saigontech.ncms.service.TeachingSwitchService;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class TeachingSwitchAction extends BaseAction {
	private Long id;
	private Long offeringId;
	private int day;
	private Long inplaceTeacherId;
	private String fromDate;
	private String toDate;
	private String comment;
	
	public String list() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		if(offering.getTeachingSwitchs().isEmpty())
			return ADD;
		REQUEST().setAttribute("offering", offering);
		return SUCCESS;
	}

	public String showAdd() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		Set<Integer> days = offering.getDays();
		List<Teacher> teachers = offering.getInstructors();
		Collection<Teacher> all = TeacherService.INSTANCE.findActiveByTeacherType(TeacherType.INSTRUCTOR);
		Set<Teacher> others = new HashSet<Teacher>();
		for(Teacher teacher : all) {
			if(!teachers.contains(teacher)) {
				others.add(teacher);
			}
		}

		REQUEST().setAttribute("offering", offering);
		REQUEST().setAttribute("teachers", teachers);
		REQUEST().setAttribute("others", others);
		REQUEST().setAttribute("days", days);
		return SUCCESS;
	}
	
	public String add() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		Date fromDateDate = DateUtil.fromString(this.fromDate, this.i18n("format.date"));
		Date toDateDate = DateUtil.fromString(this.toDate, this.i18n("format.date"));
		
		TeachingSwitch teachingSwitch = new TeachingSwitch();
		teachingSwitch.setOffering(offering);
		teachingSwitch.setFromDate(fromDateDate);
		teachingSwitch.setToDate(toDateDate);
		teachingSwitch.setMainDay(day);
		teachingSwitch.setComment(comment);
		
		Teacher teacher = offering.getInstructorOfDay(day, null);
		Teacher inplaceTeacher = TeacherService.INSTANCE.findById(inplaceTeacherId);
		
		teachingSwitch.setMainTeacher(teacher);
		teachingSwitch.setInplaceTeacher(inplaceTeacher);
		
		teachingSwitch = TeachingSwitchService.INSTANCE.persist(teachingSwitch);
		this.offeringId = offering.getId();
		
		if(teachingSwitch != null) {
			this.addLoggingDetail(BaseAction.ADD, "logging.switch.add", teacher.getFullName(), inplaceTeacher.getFullName(), offering.getName());
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String showEdit() {
		TeachingSwitch teachingSwitch = TeachingSwitchService.INSTANCE.findById(this.id);
		
		Offering offering = teachingSwitch.getOffering();
		Set<Integer> days = offering.getDays();
		List<Teacher> teachers = offering.getInstructors();
		Collection<Teacher> all = TeacherService.INSTANCE.findActiveByTeacherType(TeacherType.INSTRUCTOR);
		Set<Teacher> others = new HashSet<Teacher>();
		for(Teacher teacher : all) {
			if(!teachers.contains(teacher)) {
				others.add(teacher);
			}
		}
		
		REQUEST().setAttribute("teachers", teachers);
		REQUEST().setAttribute("others", others);
		REQUEST().setAttribute("days", days);
		REQUEST().setAttribute("model", teachingSwitch);
		REQUEST().setAttribute("offering", offering);
		return SUCCESS;
	}
	
	public String edit() {
		TeachingSwitch teachingSwitch = TeachingSwitchService.INSTANCE.findById(this.id);
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		
		Date fromDateDate = DateUtil.fromString(this.fromDate, this.i18n("format.date"));
		Date toDateDate = DateUtil.fromString(this.toDate, this.i18n("format.date"));
		
		teachingSwitch.setFromDate(fromDateDate);
		teachingSwitch.setToDate(toDateDate);
		teachingSwitch.setMainDay(day);
		teachingSwitch.setComment(comment);
		
		Teacher teacher = offering.getInstructorOfDay(day, null);
		Teacher inplaceTeacher = TeacherService.INSTANCE.findById(inplaceTeacherId);
		
		teachingSwitch.setMainTeacher(teacher);
		teachingSwitch.setInplaceTeacher(inplaceTeacher);
		
		teachingSwitch = TeachingSwitchService.INSTANCE.persist(teachingSwitch);
		this.offeringId = offering.getId();
		
		if(teachingSwitch != null) {
			this.addLoggingDetail(BaseAction.EDIT, "logging.switch.edit", teacher.getFullName(), inplaceTeacher.getFullName(), offering.getName());
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String delete() {
		TeachingSwitch teachingSwitch = TeachingSwitchService.INSTANCE.findById(this.id);
		String offeringName = teachingSwitch.getOffering().getName();
		String teacherName = teachingSwitch.getMainTeacher().getFullName();
		String inplace = teachingSwitch.getInplaceTeacher().getFullName();

		if(TeachingSwitchService.INSTANCE.deleteById(this.id)) {
			this.addLoggingDetail(BaseAction.DELETE, "logging.switch.delete", teacherName, inplace, offeringName);
			return SUCCESS;
		}
		return ERROR;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getOfferingId() {
		return offeringId;
	}

	public void setOfferingId(Long offeringId) {
		this.offeringId = offeringId;
	}

	public int getDay() {
		return day;
	}

	public void setDay(int day) {
		this.day = day;
	}

	public Long getInplaceTeacherId() {
		return inplaceTeacherId;
	}

	public void setInplaceTeacherId(Long inplaceTeacherId) {
		this.inplaceTeacherId = inplaceTeacherId;
	}

	public String getFromDate() {
		return fromDate;
	}

	public void setFromDate(String fromDate) {
		this.fromDate = fromDate;
	}

	public String getToDate() {
		return toDate;
	}

	public void setToDate(String toDate) {
		this.toDate = toDate;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}
}
